#include <conio.h>
#include <stdlib.h>
#include <windows.h>
using namespace std;
//#include <windows.h>
#include "classmenu.h"
#include "player.h"
#include "Kurcaci.h"
#include "Aqua.h"
#include "Bolt.h"
#include "Clash.h"
#include "Lahan.h"
#include "Times.h"
#include "story.h"

Menu::Menu()
{
    IsSaved = 0;
}

Menu::~Menu()
{

}

void Menu::PrintHelp()
{
    if (P.GetPlace() == 0)
    {
        cout << "You only can go to field from your house" << endl;
        cout << "Type \"teleport field\" to go to field" << endl;
    }
    else if (P.GetPlace() == 1)
    {
        cout << "You can go home and go shopping from your field" << endl;
        cout << "Type \"teleport house\" to go home" << endl;
        cout << "Type \"teleport shop\" to go shopping" << endl;
    }
    else
    {
        cout << "You only can go to field from the shop" << endl;
        cout << "Type \"teleport field\" to go field" << endl;
    }
}

void Menu::New(char playername[30])
{
    Player NewP(playername);
    P = NewP;
    I.SetMoney(2000);
    for(int i = 0 ; i <= 2 ; i++)
    {
        if (I.GetInvent(i).amount != 0)
        {
            I.DeleteInventory(I.GetKodeBarang(i),I.GetInvent(i).amount);
        }
    }
    S.SetPart(0);
    T.SetDay(0);
    T.SetClock(0);
    T.SetSeason(0);
    A.Sleep();
    B.Sleep();
    C.Sleep();
    for(int i = 1; i <= 7; i++)
    {
        for (int j = 1; j <= 7; j++)
        {
            L.setStatus(i, j, 1);
            if((L.getStatus(i, j) != 1) && (L.getStatus(i, j) != 2))
            {
                L.deleteTanaman(i, j);
            }
        }
    }
}

void Menu::Save()
{
    //Nama
    //Uang
    //Barang di inventory
    //Status Lahan
    //Status Tanaman
    //Status Kurcaci
    char player[50];
    int i;
    char *namapemain = P.GetName();
    ofstream palayer;
    palayer.open(strcat(namapemain,".abc"));
    //strtok(namapemain,".abc");
    namapemain[strlen(namapemain) - 4] = '\0';
    palayer << namapemain << endl;//namaplayer
    palayer << S.GetPart() << endl;
    palayer << T.GetClock() << " " << T.GetDay() << " " << T.GetSeason() << " " << T.GetEvent() << endl;
    palayer << I.GetMoney() << endl;//jumlah uang
    for (int x = 0; x <=2 ; x++)//kodebarang dan amount
        palayer << I.GetInvent(x).kodebarang << " " << I.GetInvent(x).amount << endl;
    for(int i = 1; i <=7 ; i++)//status lahan
    {
        for(int j = 1; j <=7 ; j++)
        {
            palayer << L.getStatus(i,j) << " ";//print status lahan
            if((L.getStatus(i,j) == 3) || (L.getStatus(i,j) == 4) || (L.getStatus(i,j) == 5) || (L.getStatus(i,j) == 6) || (L.getStatus(i,j) == 7) || (L.getStatus(i, j) == 8))
            {
                palayer << L.getTanaman(i,j).GetKodeTanaman() << " ";//print Kode tanaman di koordinat i,j
                palayer << L.getTanaman(i,j).CheckHappyMeter() << " ";//print Kondisi tanaman di koordinat i,j
                palayer << L.getTanaman(i,j).GetAge() << " ";//print Umur tanaman di koordinat i,j
                palayer << L.getTanaman(i,j).GetNHarvested() << " ";//print Nharvested dari tanaman di koordinat i,j
                if(L.getTanaman(i,j).GetNHarvested() != -999)
                {
                    palayer << L.getTanaman(i,j).GetTitikPanen() << " ";
                }
            }
            palayer << "" << endl;
        }
    }
    palayer << A.GetX() << " " << A.GetY() << " " << A.IsWakeUp() << " " << A.IsTargetting() << " " << endl;
    if(B.IsHolding() == true)
        palayer << B.GetX() << " " << B.GetY() << " " << B.IsWakeUp() << " " << B.IsTargetting() << " " << B.IsHolding() << " " << B.GetHoldCode() << " " << endl;
    else
        palayer << B.GetX() << " " << B.GetY() << " " << B.IsWakeUp() << " " << B.IsTargetting() << " " << B.IsHolding() << " " << endl;
    palayer << C.GetX() << " " << C.GetY() << " " << C.IsWakeUp() << " " << C.IsTargetting() << " " << endl;
    palayer.close();
    IsSaved = 1;
    cout << "Game successfully saved!" << endl;
    Sleep(1000);
}

int Menu::Load(char filename[100])
{
    int maks = 100;
    int money;
    char penampung[maks+1];
    char* ambil;
    char* ambil2;
    char* ambil3;
    char* ambil4;
    char* ambil5;
    ifstream palayer;
    palayer.open(filename);
    if (palayer.fail())
    {
        cerr << filename << " not found!" << endl;
        Sleep(1000);
        return 0;
    }
    else
    {
        palayer.getline(penampung,maks);
        Player NewP(penampung);
        P = NewP;
        palayer.getline(penampung,maks);
        S.SetPart(atoi(penampung));
        palayer.getline(penampung,maks);
        ambil = strtok(penampung, " ");
        ambil2 = strtok(NULL, " ");
        ambil3 = strtok(NULL, " ");
        ambil4 = strtok(NULL, " ");
        //cout << "day = " << ambil << endl;
        //cout << "season = " << ambil2 << endl;
        //cout << "event = " << ambil3 << endl;
        T.SetClock(atoi(ambil));
        T.SetDay(atoi(ambil2));
        T.SetSeason(atoi(ambil3));
        T.SetEvent(atoi(ambil4));
        
        palayer.getline(penampung,maks);
        //cout << "money = " << penampung << endl;
        I.SetMoney(atoi(penampung));

        for(int i = 0 ; i <= 2 ; i++)
        {
            if (I.GetInvent(i).amount != 0)
            {
                I.DeleteInventory(I.GetKodeBarang(i),I.GetInvent(i).amount);
            }
            palayer.getline(penampung,maks);
            ambil = strtok(penampung," ");
            ambil2 = strtok(NULL," ");
            int a = atoi(ambil);
            int b = atoi(ambil2);
            I.InsertInventory(a,b);
        }
        //I.PrintInventory();

        for(int i = 1 ; i <= 7 ; i++)
        {
            for(int j = 1 ; j <= 7 ; j++)
            {
                palayer.getline(penampung,maks);
                ambil = strtok(penampung," ");
                L.setStatus(i,j,atoi(ambil));
                if((atoi(ambil) != 1) && (atoi(ambil) != 2))
                {
                    ambil2 = strtok(NULL," ");
                    ambil3 = strtok(NULL," ");
                    ambil4 = strtok(NULL," ");
                    ambil5 = strtok(NULL," ");
                    L.LoadTanaman(i,j,atoi(ambil2));
                    L.getTanaman(i,j).SetHappyMeter(atoi(ambil3));
                    L.getTanaman(i,j).SetAge(atoi(ambil4));
                    L.getTanaman(i,j).SetNHarvested(atoi(ambil5));
                    if (atoi(ambil5) != -999)
                    {
                        ambil5 = strtok(NULL," ");
                        L.getTanaman(i,j).SetTitikPanen(atoi(ambil5));
                    }
                    //cout << "status: " << ambil << ", tanaman: " << ambil2 << ", happymeter: " << ambil3 << ", age: " << ambil4 << endl;
                }
            }
        }

        palayer.getline(penampung,maks);
        ambil = strtok(penampung," ");
        ambil2 = strtok(NULL," ");
        ambil3 = strtok(NULL," ");
        ambil4 = strtok(NULL," ");
        if(atoi(ambil3) == 1)
        {
            A.WakeUp();
            A.SetX(atoi(ambil));
            A.SetY(atoi(ambil2));
        }
        palayer.getline(penampung,maks);
        ambil = strtok(penampung," ");
        ambil2 = strtok(NULL," ");
        ambil3 = strtok(NULL," ");
        ambil4 = strtok(NULL," ");
        if(atoi(ambil3) == 1)
        {
            B.WakeUp();
            B.SetX(atoi(ambil));
            B.SetY(atoi(ambil2));
            B.CommandHold(atoi(ambil4));
            if (atoi(ambil4) == 1)
            {
                ambil5 = strtok(NULL," ");
                B.SetHoldCode(atoi(ambil5));
            }
        }

        palayer.getline(penampung,maks);
        ambil = strtok(penampung," ");
        ambil2 = strtok(NULL," ");
        ambil3 = strtok(NULL," ");
        ambil4 = strtok(NULL," ");
        if(atoi(ambil3) == 1)
        {
            C.WakeUp();
            C.SetX(atoi(ambil));
            C.SetY(atoi(ambil2));
        }
        palayer.close();
        cout << "Loading " << filename;
        for (int i = 1; i <= 3; i++)
        {
           cout << "."; Sleep(500);
        }
        IsSaved = 1;
        return 1;
    }
}

void Menu::BackToMenu()
{
    char c;
    if (!IsSaved)
    {
        do
        {
            cout << "Your game is not saved. Do you want to save game?(y/n)";
            c = getch();
            if (c == 'y' || c == 'Y')
            {
                cout << endl << "Saving";
                for (int i = 1; i <= 3; i++)
                {
                    cout << ".";
                    Sleep(300);
                }
                Save();
            }
        } while(c != 'y' && c != 'Y' && c != 'n' && c != 'N');
    }
    PlaySound(TEXT("music/menu.wav"), NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);
}

void Menu::MainGame()
{
    int NowPlaying = 0;
    int IsBackToMenu = 0;
    do
    {
        switch(P.GetPlace())
        {
            case 0:
                if (S.GetPart() == 0)
                    S.MainStory();
                if (NowPlaying != 5)
                    {PlaySound(TEXT("music/house.wav"), NULL, SND_FILENAME | SND_ASYNC | SND_LOOP); NowPlaying = 5;}
                S.PrintHouse();
                IsBackToMenu = GetCommandHouse();
                break;
            case 1:
                if (T.GetSeason() == 0 && NowPlaying != 1) { PlaySound(TEXT("music/spring.wav"), NULL, SND_FILENAME | SND_ASYNC | SND_LOOP); NowPlaying = 1;}
                else if (T.GetSeason() == 1 && NowPlaying != 2) { PlaySound(TEXT("music/summer.wav"), NULL, SND_FILENAME | SND_ASYNC | SND_LOOP); NowPlaying = 2;}
                else if (T.GetSeason() == 2 && NowPlaying != 3) { PlaySound(TEXT("music/fall.wav"), NULL, SND_FILENAME | SND_ASYNC | SND_LOOP); NowPlaying = 3;}
                else if (T.GetSeason() == 3 && NowPlaying != 4) { PlaySound(TEXT("music/winter.wav"), NULL, SND_FILENAME | SND_ASYNC | SND_LOOP); NowPlaying = 4;}
                L.PrintLahanKosong();
                L.PrintLahan();
                S.PrintFieldInfo();
                GetCommandField();
                break;
            case 2:
                if (S.GetPart() == 1)
                {
                    S.MainStory();
                    P.Teleport(0);
                    T.ChangeDay();
                }
                else
                {
                    if(NowPlaying != 6)
                    {
                        PlaySound(TEXT("music/shop.wav"), NULL, SND_FILENAME | SND_ASYNC | SND_LOOP); NowPlaying = 6;
                    }
                    S.PrintShop();
                    GetCommandShop();
                }
                break;
        }
    } while (!IsBackToMenu);
}

int Menu::GetCommandMenu()
{
    char command[50];
    char *pch;
    cin.getline(command,'#');                                       // baca input user
    if (strstr(command, "#"))                                       // cek apakah ada '#'
    {
        pch = strtok(command, " #");                                // get first word
        //cout << "pch = '" << pch << "'" << endl;
        if (!stricmp(pch, "new"))
        {
            pch = strtok(NULL, "#");                                // get player name
            while (pch[strlen(pch) - 1] == ' ')                     // ignore ' ' behind player name
                pch[strlen(pch) - 1] = '\0';
            New(pch);
            cout << "Welcome, " << pch << "!" << endl;
            Sleep(1000);
            MainGame();
            return 0;
        }
        else if (!stricmp(pch, "load"))
        {
            pch = strtok(NULL, "#");                                // get file name
            while (pch[strlen(pch) - 1] == ' ')                     // ignore ' ' behind file name
                pch[strlen(pch) - 1] = '\0';
            if (Load(pch))
            {
                MainGame();
            }
            return 0;
        }
        else if (!stricmp(pch, "exit"))
        {
            return 1;
        }
        else
        {
            cout << "Invalid command!" << endl;
            Sleep(1000);
            return 0;
        }
    }
    else
    {
        cerr << "Invalid input, mark not found!" << endl;
        Sleep(1000);
        return 0;
    }
}


int Menu::GetCommandHouse()
{
    char command[50];
    char *pch;
    cin.getline(command,'#');                                       // baca input user
    if (strstr(command, "#"))                                       // cek apakah ada '#'
    {
        pch = strtok(command, " #");                                // get first word
        if (!stricmp(pch, "save"))
        {
            cout << "Saving to " << P.GetName() << ".abc";
            for (int i = 1; i <= 3; i++)
            {
                cout << ".";
                Sleep(1000);
            }
            Save();
            return 0;
        }
        else if (!stricmp(pch, "sleep"))
        {
            cout << "It's time to sleep..." << endl;
            Sleep(1000);
            for(int i = T.GetNormDay() - T.GetClock() - 1; i > 0; i--)
            {
                T.IncClock();
            }
            T.ChangeDay();
            T.RandomEvent();
            M.IsSaved = 0;
            return 0;
        }
        else if (!stricmp(pch, "menu"))
        {
            BackToMenu();
            return 1;
        }
        else if (!stricmp(pch, "inventory"))
        {
            I.PrintInventory();
            return 0;
        }
        else if (!stricmp(pch, "eat"))
        {
            int slot;
            pch = strtok(NULL, "#");                                // get slot
            slot = atoi(pch);
            if ((slot == 0) || (slot == 1) || (slot == 2))          // validasi slot
            {
                P.Eat(slot);
                T.IncClock();
            }
            else
            {
                cerr << "Invalid input! Your slot are <0, 1, 2>";
                Sleep(1000);
            }
            return 0;
        }
        else if (!stricmp(pch, "teleport"))
        {
            pch = strtok(NULL, " #");                             // get teleport option
            if (!stricmp(pch, "field"))
            {
                cout << "Prepare to go to field..." << endl;
                Sleep(1000);
                P.Teleport(1);
                P.SetPosition(0, 0);
                P.ChangeDirect('>');
                T.IncClock();
            }
            else if (!stricmp(pch, "-help"))
            {
                PrintHelp();
                getch();
            }
            return 0;
        }
        else
        {
            cout << "Invalid input!" << endl;
            Sleep(1000);
            return 0;
        }
    }
    else
    {
        cout << "Invalid input, mark not found!" << endl;
        Sleep(1000);
        return 0;
    }
}

void Menu::GetCommandField()
{
    char command[50];
    char *pch;
    cin.getline(command,'#');                                       // baca input user
    if (strstr(command, "#"))                                       // cek apakah ada '#'
    {
        pch = strtok(command, " #");                                // get first word
        if (!stricmp(pch, "easy"))
        {
            L.PrintLahanKosong();
            cout << endl;
            cout << "Press direction button to move" << endl;
            cout << "Press 'p' to plow" << endl;
            cout << "Press 's' to slash" << endl;
            cout << "Press 'w' to water" << endl;
            cout << "Press 'h' to harvest" << endl;
            cout << "Press enter to exit easy mode" << endl;
            L.PrintLahan();
            char c;
            int tDay = T.GetDay();
            do
            {
                L.PrintLahan();
                gotoxy(0, 51);
                c = getch();
                switch (c)
                {
                    case 72 : P.Move('^');
                              T.IncClock();
                              break;
                    case 80 : P.Move('v');
                              T.IncClock();
                              break;
                    case 77 : P.Move('>');
                              T.IncClock();
                              break;
                    case 75 : P.Move('<');
                              T.IncClock();
                              break;
                    case 'p': P.Plow();
                              break;
                    case 's': P.Slash();
                              break;
                    case 'w': P.Water();
                              break;
                    case 'h': P.Harvest();
                              break;
                }
                gotoxy(0, 51);
                cout << "                                             " << endl;
            } while((c != 13) && (tDay == T.GetDay()));
        }
        else if (!stricmp(pch, "wake_up"))
        {
            int n;
            pch = strtok(NULL, " #");                             // get dwarf number
            n = atoi(pch);
            if (n == 1)
            {
                A.WakeUp();
                M.IsSaved = 0;
            }
            else if (n == 2)
            {
                B.WakeUp();
                M.IsSaved = 0;
            }
            else if (n == 3)
            {
                C.WakeUp();
                M.IsSaved = 0;
            }
        }
        else if (!stricmp(pch, "right"))
        {
            int i, n;
            pch = strtok(NULL, " #");                             // get n
            n = atoi(pch);
            for (i = 1; i <= n; i++)
            {
                P.Move('>');
                T.IncClock();
            }
        }
        else if (!stricmp(pch, "left"))
        {
            int i, n;
            pch = strtok(NULL, " #");                             // get n
            n = atoi(pch);
            for (i = 1; i <= n; i++)
            {
                P.Move('<');
                T.IncClock();
            }
        }
        else if (!stricmp(pch, "up"))
        {
            int i, n;
            pch = strtok(NULL, " #");                             // get n
            n = atoi(pch);
            for (i = 1; i <= n; i++)
            {
                P.Move('^');
                T.IncClock();
            }
        }
        else if (!stricmp(pch, "down"))
        {
            int i, n;
            pch = strtok(NULL, " #");                             // get n
            n = atoi(pch);
            for (i = 1; i <= n; i++)
            {
                P.Move('v');
                T.IncClock();
            }
        }
        else if (!stricmp(pch, "plow"))
        {
            P.Plow();
            T.IncClock();
        }
        else if (!stricmp(pch, "put"))
        {
            char *pch2 = "0";
            int slot, jumlah;
            pch = strtok(NULL, " #");                             // get slot
            pch2 = strtok(NULL, " #");                             // get jumlah
            slot = atoi(pch);
            jumlah = atoi(pch2);
            //cout << "slot = " << slot << ", jml = " << jumlah << endl;
            if (jumlah == 1)
                P.Put(slot, jumlah);
            T.IncClock();
        }
        else if (!stricmp(pch, "slash"))
        {
            P.Slash();
            T.IncClock();
        }
        else if (!stricmp(pch, "water"))
        {
            P.Water();
            T.IncClock();
        }
        else if (!stricmp(pch, "harvest"))
        {
            P.Harvest();
            T.IncClock();
        }
        else if (!stricmp(pch, "sleep"))
        {
            int n;
            pch = strtok(NULL, " #");                             // get dwarf number
            n = atoi(pch);
            if (n == 1)
            {
                A.Sleep();
                M.IsSaved = 0;
            }
            else if (n == 2)
            {
                B.Sleep();
                M.IsSaved = 0;
            }
            else if (n == 3)
            {
                C.Sleep();
                M.IsSaved = 0;
            }
        }
        else if (!stricmp(pch, "status"))
        {
            cout << "DWARF STATUS" << endl;
            cout << "1. Aqua: ";
            A.PrintStatus();
            cout << "2. Bolt: ";
            B.PrintStatus();
            cout << "3. Clash: ";
            C.PrintStatus();
            getch();
        }
        else if (!stricmp(pch, "info"))
        {
            S.PrintFieldCharInfo();
            getch();
        }
        else if (!stricmp(pch, "inventory"))
        {
            I.PrintInventory();
        }
        else if (!stricmp(pch, "eat"))
        {
            char *pch;
            int slot;
            pch = strtok(NULL, " #");                             // get slot
            slot = atoi(pch);
            if ((slot == 0) || (slot == 1) || (slot == 2))                               // validasi slot
            {
                P.Eat(slot);
                T.IncClock();
            }
            else
            {
                cerr << "Invalid input! Your slot are <0, 1, 2>";
                Sleep(1000);
            }
        }
        else if (!stricmp(pch, "teleport"))
        {
            pch = strtok(NULL, " #");                             // get teleport option
            if (!stricmp(pch, "house"))
            {
                if ((P.GetPosition().x == 0) && (P.GetPosition().y == 0))
                {
                    cout << "Prepare to go home..." << endl;
                    Sleep(1000);
                    P.Teleport(0);
                    T.IncClock();
                }
                else
                {
                    cerr << "Can't teleport from here!" << endl;
                    Sleep(1000);
                }
            }
            else if (!stricmp(pch, "shop"))
            {
                if ((P.GetPosition().x == 0) && (P.GetPosition().y == 0))
                {
                    cout << "Prepare to go to shop..." << endl;
                    Sleep(1000);
                    P.Teleport(2);
                    T.IncClock();
                }
                else
                {
                    cerr << "Can't teleport from here!" << endl;
                    Sleep(1000);
                }
            }
            else if (!stricmp(pch, "-help"))
            {
                PrintHelp();
                getch();
            }
        }
        else
        {
            cout << "Invalid input!" << endl;
            Sleep(1000);
        }
    }
    else
    {
        cout << "Invalid input, mark not found!" << endl;
        Sleep(1000);
    }
}

void Menu::GetCommandShop()
{
    char command[50];
    char *pch;
    cin.getline(command,'#');                                       // baca input user
    if (strstr(command, "#"))                                       // cek apakah ada '#'
    {
        pch = strtok(command, " #");                                // get first word
        if (!stricmp(pch, "store"))
        {
            S.PrintStore();
            getch();
        }
        else if (!stricmp(pch, "buy"))
        {
            char *pch2;
            int kode, jml;
            pch = strtok(NULL, " ");                             // get item name
            pch2 = strtok(NULL, "#");                             // get amount
            if (!stricmp(pch, "chronoplant"))
            {
                kode = 1;
            }
            else if (!stricmp(pch, "reynfruit"))
            {
                kode = 2;
            }
            else if (!stricmp(pch, "ichalmond"))
            {
                kode = 3;
            }
            else if (!stricmp(pch, "edondong"))
            {
                kode = 4;
            }
            else if (!stricmp(pch, "greenalgoe"))
            {
                kode = 5;
            }
            else if (!stricmp(pch, "hanselnut"))
            {
                kode = 6;
            }
            else if (!stricmp(pch, "abeltree"))
            {
                kode = 7;
            }
            else if (!stricmp(pch, "elmonade"))
            {
                kode = 8;
            }
            jml = atoi(pch2);
            if(kode >= 1 && kode <= 8)
            {
                char c = 143;
                char y;
                S.SetTextColour(496);
                M.gotoxy(42, 15);
                cout << "It will be " << jml*I.GetPrice(kode) << " "<< c << " ,               "<<endl;
                M.gotoxy(42, 16);
                cout<<"are you sure?(y/n)";
                S.SetTextColour(7);
                y = getch();
                cout << endl;
                if((y == 'y')||(y == 'Y'))
                {
                    I.Buy(kode, jml);
                    gotoxy(0, 6);
                    S.PrintGirlHappy2();
                    gotoxy(42, 15);
                    S.SetTextColour(496);
                    cout << "Thank you!                     " << endl;
                    S.SetTextColour(7);
                }
                Sleep(1000);
            }
            else
            {
                cout << "Item's not available" << endl;
                Sleep(1000);
            }
        }
        else if (!stricmp(pch, "sell"))
        {
            int slot, jml;
            char c = 143;
            char y;
            char *pch2;
            pch = strtok(NULL, " ");                             // get slot
            pch2 = strtok(NULL, "#");                             // get amount
            slot = atoi(pch);
            int kode = I.GetKodeBarang(slot);
            jml = atoi(pch2);
            if(I.GetKodeBarang(slot) != 0)
            {
                S.SetTextColour(496);
                M.gotoxy(42, 15);
                cout << "It will be ";
                if(I.IsBibit(slot))
                    cout << jml*I.GetPrice(kode)/2 << " " << c << " ,               ";
                else
                    cout << jml*I.GetPrice(kode) << " " << c << " ,               ";
                M.gotoxy(42, 16);
                cout<<"are you sure?(y/n)";
                S.SetTextColour(7);
                y = getch();
                cout << endl;
                if((y == 'y')||(y == 'Y'))
                {
                    gotoxy(0, 6);
                    S.PrintGirlHappy2();
                    gotoxy(42, 15);
                    S.SetTextColour(496);
                    cout << "Thank you!                     " << endl;
                    S.SetTextColour(7);
                    I.Sell(slot, jml);
                    //gotoxy(0, 28);
                    //cout << "Transaction Succesful";
                    Sleep(1000);
                }
            }
            else 
            {
                cout << "There is no item in that slot. You cannot sell anything." << endl;
                Sleep(1000);
            }
        }
        else if (!stricmp(pch, "inventory"))
        {
            I.PrintInventory();
        }
        else if (!stricmp(pch, "eat"))
        {
            int slot;
            pch = strtok(NULL, "#");                             // get slot
            slot = atoi(pch);
            if ((slot == 0) || (slot == 1) || (slot == 2))                               // validasi slot
            {
                P.Eat(slot);
                T.IncClock();
            }
            else
            {
                cerr << "Invalid input! Your slot are <0, 1, 2>";
                Sleep(1000);
            }
        }
        else if (!stricmp(pch, "teleport"))
        {
            pch = strtok(NULL, "#");                             // get teleport option
            if (!stricmp(pch, "field"))
            {
                cout << "Prepare to go to field..." << endl;
                Sleep(1000);
                P.Teleport(1);
                P.SetPosition(0, 0);
                P.ChangeDirect('>');
                T.IncClock();
            }
            else if (!stricmp(pch, "-help"))
            {
                PrintHelp();
                getch();
            }
        }
        else
        {
            cout << "Invalid input!" << endl;
            Sleep(1000);
        }
    }
    else
    {
        cout << "Invalid input, mark not found!" << endl;
        Sleep(1000);
    }
}


void Menu::PrintStat()
{
    gotoxy(41,0);
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x47);
    cout << "|                                      ";
    gotoxy(41, 1);
    cout << "|\t\t" << P.GetName() << "\t\t\t";
    gotoxy(41, 2);
    cout << "|                                      ";
    gotoxy(41, 3);
    char c = 143;
    cout << "|\t\t" << I.GetMoney() << " " << c << "\t\t\t";
    gotoxy(41, 4);
    cout << "|______________________________________" << endl;
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x0F);
}

void Menu::UpdateStat()
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x47);
    gotoxy(56, 1);
    cout << P.GetName();
    gotoxy(56, 3);
    char c = 143;
    cout << I.GetMoney() << " " << c << "     ";
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x0F);
}

void Menu::gotoxy(int x,int y)
{
    HANDLE hConsoleOutput;
    COORD dwCursorPosition;
    dwCursorPosition.X = x;
    dwCursorPosition.Y = y;
    hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hConsoleOutput,dwCursorPosition);
}
